package com.dream.service.user.web.processor.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.bean.SerVersionBean;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.common.toolkits.clickstream.ClickStreamClient;
import com.dream.common.toolkits.clickstream.bean.ClickStreamBaseRequest;
import com.dream.common.toolkits.clickstream.bean.ExtradataBase;
import com.dream.common.toolkits.clickstream.bean.LogType;
import com.dream.common.toolkits.clickstream.bean.SkipType;
import com.dream.service.user.api.UserCollectionService;
import com.dream.service.user.domain.entity.IsFollowCollect;
import com.dream.service.user.web.model.FieldSchema;
import com.dream.service.user.web.model.Param;
import com.dream.service.user.web.processor.RequestProcessor;
import com.dream.service.user.web.util.UserWebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Created by kangd on 2016/5/4.
 */
public class UserCollectionRequestProcessor implements RequestProcessor {

    UserCollectionService userCollectionService;

    public UserCollectionService getUserCollectionService() {
        return userCollectionService;
    }

    public void setUserCollectionService(UserCollectionService userCollectionService) {
        this.userCollectionService = userCollectionService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {
        JSONObject result = new JSONObject();
        String cmd = req.getParameter(Param.CMD);
        try {
            if (cmd.equals(Param.UserCollectionCMD.USER_COLLECTION_Add)) {
                this.addWishList(req, result);
            }else if (cmd.equals(Param.UserCollectionCMD.USER_COLLECTION_DEL)) {
                this.delWishList(req, result);
            }else if (cmd.equals(Param.UserCollectionCMD.USER_COLLECTION_LIST)) {
                this.getWishList(req, result);
            }else if (cmd.equals(Param.UserCollectionCMD.USER_COLLECTION_IS)) {
                this.isWish(req, result);
            }
//            else if (cmd.equals(Param.UserCollectionCMD.USER_COLLECTION_ONLYSPU)) {
//                this.onlySpusList(req, result);
//            }
            else {
                AppResultBuilder.buildCmdErrorResult(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildResult(result,"",false, ErrorCode.UNKOWN);
        }
        return result;
    }


    // http://motif.evermarker.com/wishlist?cmd=add&pin=95098c62df274bf882b931b4d2b2e33c&spu=10000024
    //增加 wishlist
    public void addWishList(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        String spuStr = req.getParameter(FieldSchema.SPU);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(spuStr) ){
            AppResultBuilder.buildParamErrorResult(result,"pin or spu null","");
            return;
        }
        Long spu = Long.valueOf(spuStr);
        BaseResponse<Boolean> response = null;
        try {
            response = userCollectionService.addWishList(pin, Arrays.asList(spu));
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result,new Object());
            }else{
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(),"");
            }


        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }finally {
          try{
             //加入wishlist埋点
             SerVersionBean serVersionBean= UserWebUtil.buildSerVersionBean(req);
             ClickStreamClient clickStreamClient = new ClickStreamClient();
             ExtradataBase extradataBase = new ExtradataBase(SkipType.PRODUCT,spuStr);
             ClickStreamBaseRequest clickStreamBaseRequest = new ClickStreamBaseRequest(LogType.WISHLIST_ADD,
                     req.getParameter(FieldSchema.UUID),pin,serVersionBean.getSrc(),
                     serVersionBean.getVer(),serVersionBean.getVersion());
             clickStreamClient.reportClickStream(clickStreamBaseRequest,extradataBase);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // http://motif.evermarker.com/wishlist?cmd=del&pin=95098c62df274bf882b931b4d2b2e33c&spu=10000024
    //删除 wishlist
    public void delWishList(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        String spuStr = req.getParameter(FieldSchema.SPU);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(spuStr) ){
            AppResultBuilder.buildParamErrorResult(result,"pin or spu null","");
            return;
        }
        Long spu = Long.valueOf(spuStr);
        BaseResponse<Boolean> response = null;
        try {
            response = userCollectionService.delWishList(pin, Arrays.asList(spu));
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result,new Object());
            }else{
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(),"");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }finally {
            try {
                //加入wishlist埋点
                SerVersionBean serVersionBean=UserWebUtil.buildSerVersionBean(req);
                ClickStreamClient clickStreamClient = new ClickStreamClient();
                ExtradataBase extradataBase = new ExtradataBase(SkipType.PRODUCT,String.valueOf(spu));
                ClickStreamBaseRequest clickStreamBaseRequest = new ClickStreamBaseRequest(LogType.WISHLIST_DEL,
                        req.getParameter(FieldSchema.UUID),pin,serVersionBean.getSrc(),
                        serVersionBean.getVer(),serVersionBean.getVersion());
                clickStreamClient.reportClickStream(clickStreamBaseRequest,extradataBase);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    // http://motif.evermarker.com/wishlist?cmd=list&pin=95098c62df274bf882b931b4d2b2e33c&num=1&size=20
    //拉取 wishlist
    public void getWishList(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        if(StringUtils.isEmpty(pin) ){
            AppResultBuilder.buildParamErrorResult(result,"pin null","");
            return;
        }

        //分页
        Integer pageNum=null;
        Integer pageSize=null;
        String pageNumString = req.getParameter(FieldSchema.PAGE_NUM);
        String pageSizeString = req.getParameter(FieldSchema.PAGE_SIZE);
        if(pageNumString!=null && pageSizeString != null){
            pageNum = Integer.valueOf(pageNumString);
            pageSize = Integer.valueOf(pageSizeString);
            if(pageNum<=0 || pageSize<0){
                AppResultBuilder.buildParamErrorResult(result,"pageNum or PageSize Error","");
                return;
            }
        }
        BaseListResponse baseListResponse = null;
        try {
            baseListResponse = userCollectionService.getWishList(pin,pageNum,pageSize);
            if (baseListResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseListResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseListResponse.getError_code(), baseListResponse.getError_msg(),"");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

    //判断是否加入 colloction
    private void isWish(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        String spuStr = req.getParameter(FieldSchema.SPU);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(spuStr) ){
            AppResultBuilder.buildParamErrorResult(result,"pin or spu null","");
            return;
        }
        Long spu = Long.valueOf(spuStr);
        BaseResponse<IsFollowCollect> response;
        try {
            response = userCollectionService.iscollect(pin, spu);
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result, response.getResult());
            } else {
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }


    private void onlySpusList(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        try {
            BaseListResponse response = userCollectionService.getWishListOnlySpu(pin);
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result, response.getResult());
            } else {
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }
}
